﻿using System;
using System.Drawing;
using System.Windows.Forms;

using SuperMap.Data;
using SuperMap.Mapping;
using SuperMap.UI;

namespace SuperMapLayerGroupSample
{
    public partial class MainForm : Form
    {
        // 测试数据
        private string dataPath = "../../SampleData/test.udbx";

        private Workspace workspace;
        private LayersTree layersTree;
        private MapControl mapControl;

        public MainForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            helpProvider.SetHelpString(splitContainer.Panel1, "鼠标右键点击图层进行分组操作");
            helpProvider.SetShowHelp(splitContainer.Panel1, true);

            // 初始化工作空间
            DatasourceConnectionInfo datasourceConnectionInfo = new DatasourceConnectionInfo
            {
                Server = dataPath,
                EngineType = EngineType.UDBX,
                Alias = "testData"
            };
            workspace = new Workspace();
            Datasource datasource = workspace.Datasources.Open(datasourceConnectionInfo);

            InitLayouts();
            InitLayersTreeContextMenuStrip();

            mapControl.Map.Workspace = workspace;
            layersTree.Map = mapControl.Map;

            // 向地图添加测试图层
            foreach (Dataset dataset in datasource.Datasets)
            {
                mapControl.Map.Layers.Add(dataset, new LayerSettingVector(), true);
            }
        }

        /// <summary>
        /// 初始化控件布局
        /// </summary>
        private void InitLayouts()
        {
            // 地图控件布局
            mapControl = new MapControl()
            {
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom,
                Bounds = new Rectangle(0, 0, splitContainer.Panel2.Bounds.Width, splitContainer.Panel2.Bounds.Height)
            };
            splitContainer.Panel2.Controls.Add(mapControl);

            // 图层控件布局
            layersTree = new LayersTree()
            {
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom,
                Bounds = splitContainer.Panel1.Bounds
            };
            splitContainer.Panel1.Controls.Add(layersTree);
        }

        /// <summary>
        /// 初始化图层树右键菜单
        /// </summary>
        private void InitLayersTreeContextMenuStrip()
        {
            ContextMenuStrip layerContextMenuStrip = new ContextMenuStrip()
            {
                Tag = layersTree
            };
            layerContextMenuStrip.Items.Add("新建图层组", null, new EventHandler(CreateLayerGroup));
            layersTree.NodeContextMenuStrips.Add(LayersTreeNodeDataType.Layer, layerContextMenuStrip);

            ContextMenuStrip layerGroupContextMenuStrip = new ContextMenuStrip()
            {
                Tag = layersTree
            };
            layerGroupContextMenuStrip.Items.Add("取消图层组", null, new EventHandler(DeleteLayerGroup));
            layersTree.NodeContextMenuStrips.Add(LayersTreeNodeDataType.LayerGroup, layerGroupContextMenuStrip);
        }

        /// <summary>
        /// 新建图层组
        /// </summary>
        /// <param name="sender">点击的浮动菜单项</param>
        /// <param name="e">事件参数</param>
        private void CreateLayerGroup(object sender, EventArgs e)
        {
            if (sender is ToolStripItem toolStripItem)
            {
                Layer selectedLayer = GetSelectedLayer(toolStripItem);
                if (selectedLayer != null)
                {
                    LayerGroup layerGroup = new LayerGroup("默认分组");
                    if (selectedLayer.ParentGroup == null)
                    {
                        selectedLayer.Layers.Add(layerGroup);
                    }
                    else
                    {
                        selectedLayer.ParentGroup.Add(layerGroup);
                    }
                    layerGroup.Add(selectedLayer);
                    LayersTree layersTree = GetSelectedLayersTree(toolStripItem);
                    if (layersTree != null)
                    {
                        layersTree.SelectedNode = layersTree.FindNode(layerGroup);
                        layersTree.Interactions[InteractionType.KeyF2].DynamicInvoke(new object[] { layersTree, new KeyEventArgs(Keys.F2) });
                    }
                }
            }
        }

        /// <summary>
        /// 取消当前选中的图层组
        /// </summary>
        /// <param name="sender">点击的浮动菜单项</param>
        /// <param name="e">事件参数</param>
        private void DeleteLayerGroup(object sender, EventArgs e)
        {
            if (sender is ToolStripItem toolStripItem)
            {
                LayerGroup selectedLayerGroup = GetSelectedLayerGroup(toolStripItem);
                if (selectedLayerGroup != null)
                {
                    selectedLayerGroup.UnGroup();
                }
            }
        }

        /// <summary>
        /// 获取当前图层树对象
        /// </summary>
        /// <param name="toolStripItem">点击的浮动菜单项</param>
        /// <returns>当前图层树对象</returns>
        private LayersTree GetSelectedLayersTree(ToolStripItem toolStripItem)
        {
            LayersTree layersTree = null;
            ToolStrip toolStrip = toolStripItem.GetCurrentParent();
            if (toolStrip is ContextMenuStrip contextMenuStrip)
            {
                if (contextMenuStrip.Tag is LayersTree)
                {
                    layersTree = (LayersTree)contextMenuStrip.Tag;
                }
            }
            return layersTree;
        }

        /// <summary>
        /// 获取当前选中的图层对象
        /// </summary>
        /// <param name="toolStripItem">点击的浮动菜单项</param>
        /// <returns>当前选中的图层对象</returns>
        private Layer GetSelectedLayer(ToolStripItem toolStripItem)
        {
            Layer selectedLayer = null;
            LayersTree layersTree = GetSelectedLayersTree(toolStripItem);
            if (layersTree != null)
            {
                if (layersTree.SelectedNode is LayersTreeNodeBase layersTreeNode)
                {
                    if (layersTreeNode.GetData() is Layer layer)
                    {
                        selectedLayer = layer;
                    }
                }
            }
            return selectedLayer;
        }

        /// <summary>
        /// 获取当前选中的图层组对象
        /// </summary>
        /// <param name="toolStripItem">点击的浮动菜单项</param>
        /// <returns>当前选中的图层组对象</returns>
        private LayerGroup GetSelectedLayerGroup(ToolStripItem toolStripItem)
        {
            LayerGroup selectedLayerGroup = null;
            LayersTree layersTree = GetSelectedLayersTree(toolStripItem);
            if (layersTree != null)
            {
                if (layersTree.SelectedNode is LayersTreeNodeBase layersTreeNode)
                {
                    if (layersTreeNode.GetData() is LayerGroup layerGroup)
                    {
                        selectedLayerGroup = layerGroup;
                    }
                }
            }
            return selectedLayerGroup;
        }
    }
}
